<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>所属科目：</label>
                <div class="table-query-input">
                    <category-select
                        :category-id.sync="queryData.categoryId"
                        :props="{}"
                    />
                </div>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="primary"
                plain
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="clickAdd('添加视频章节', () => {})"
            >新增章节
            </el-button>
            <el-button
                type="danger"
                plain
                style="margin-bottom: 2em"
                icon="el-icon-delete"
                :disabled="selectDisable"
                @click="
                    tableDelete(
                        '/kc/course/video/chapter/deletes',
                        {
                            ids: tableSelect
                        },
                        true
                    )
                "
            >批量删除
            </el-button>

        </div>

        <div class="table-data-box">
            <el-table
                ref="chapterTable"
                :data="chapterData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                @selection-change="tableSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="章节名称"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="createdTime"
                    label="添加时间">
                </el-table-column>
                <el-table-column
                    prop="videoNum"
                    label="视频数">
                </el-table-column>
                <el-table-column
                    prop="sort"
                    label="排序"
                    width="180">

                    <template
                        slot-scope="scope"
                        v-loading="scope.row.opLoading"
                    >
                        <div
                            v-show="!scope.row.sortEdit"
                            @dblclick="scope.row.sortEdit = true"
                        >
                            {{ scope.row.sort }}
                        </div>
                        <el-input
                            v-show="scope.row.sortEdit"
                            v-model="scope.row.sort"
                            @blur="
                                sortEditSave(
                                    '/kc/course/video/chapter/change-sort',
                                    scope.row
                                )
                            "
                        ></el-input>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="
                                clickSimpleEdit(
                                    scope.row,
                                    '数据编辑',
                                    undefined,
                                    editCover
                                )
                            "
                        >编辑
                        </el-button>
                        <el-button
                            v-if="scope.row.children == null || scope.row.children.length === 0"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-refresh"
                            :loading="scope.row.opLoading"
                            @click="changeVideoNum(scope.row.id)"
                        >更新视频数
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            custom-class="two-dialog"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="6em"
                :rules="dataFormRule"
                class="table-edit-from"

            >
                <div class="xy-between">
                    <el-form-item label="所属科目" prop="categoryId">
                        <category-select
                            :category-id.sync="dataForm.categoryId"

                            :props="{}"
                        />
                    </el-form-item>
                    <el-form-item label="上级章节" prop="parentId">
                        <el-cascader
                            :key="key"
                            v-model="dataForm.parentId"
                            v-loading="chapterFlag"
                            :show-all-levels="false"
                            clearable
                            filterable
                            :props="{
                                value: 'id',
                                label: 'name',
                                checkStrictly: true
                            }"
                            :options="chapterData"
                            @change="
                                vl =>
                                    selectParent(
                                        vl,
                                        dataForm,
                                        dataForm.parent || tableData
                                    )
                            "
                        ></el-cascader>
                    </el-form-item>
                </div>
                <div class="row">
                    <el-form-item label="章节名称" prop="name">
                        <el-input v-model="dataForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="章节别名" prop="serverName">
                        <el-input v-model="dataForm.serverName"></el-input>
                    </el-form-item>
                </div>
                <div class="row">
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="dataForm.sort"></el-input>
                    </el-form-item>
                </div>

                <div class="row">
                    <el-form-item label="章节备注" prop="remark">
                        <el-input
                            v-model="dataForm.remark"
                            type="textarea"
                        ></el-input>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="
                        formDataSave(
                            '/kc/course/video/chapter/update',
                            saveDataCover,
                            () => {}
                        )
                    "
                >保 存</el-button
                >
            </span>
        </el-dialog>

    </div>
</template>

<script>
import {execCall, TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import CategorySelect from "@/components/CategorySelect.vue";
import {changeVideoNum} from "@/api/course";

export default {
    name: "videoChapterList",
    components: {CategorySelect},
    mixins: [TablePage],
    data() {
        let self = this;
        return {
            queryData: {
                categoryId: null
            },
            dataFormRule: {
                categoryId: [
                    {
                        required: true,
                        message: "请选择科目",
                        trigger: "change"
                    }
                ],
                name: [
                    {
                        required: true,
                        message: "请输入章节名称",
                        trigger: "blur"
                    }
                ]
            },
            selectedRows: []

        };
    },
    computed: {
        ...mapState({
            aliyunVideoStatus: state => state.status.aliyunVideoStatus,
        })
    },
    watch: {
        "dataForm.categoryId"(categoryId) {
            if (!this.dataForm.opLoading) {
                this.dataForm.parentId = null;
            }
            if (!categoryId) {
                this.chapterData = [];
            } else {
                this.key++;
                this.loadChapterTree(categoryId);
            }
        }

    },
    created() {
        this.loadCate({cateId:-1},(category) => {
            if (category !== null) {
                this.cateList = category
            }
        });
    },
    mounted() {

    },
    methods: {

        saveDataCover(data) {
            console.log("data",data)
            if (data.parent && typeof data.parent === "object") {
                data.parent = null;
            }

            if (data.children && typeof data.children === "object") {
                data.children = null;
            }
            return {
                ...data,
                //parentId: data.parentId === null ? 0 : data.parentId[data.parentId.length-1]
            };
        },
        clickSearch() {
            if(this.queryData.categoryId === null || this.queryData.categoryId === undefined){
                return this.$message.warning("请先选择所属科目");
            }
            this.loadChapterTree(this.queryData.categoryId) ;

        },
        handleSelectionChange(selection) {
            this.selectedRows = selection;
            this.syncChildrenSelection(selection);
        },
        syncChildrenSelection(selection) {
            const traverse = (nodes, selected) => {
                nodes.forEach(node => {
                    if(this.tableSelect === null || this.tableSelect.length === 0){
                        this.tableSelect = [];
                    }
                    this.tableSelect.push(node.id);
                    this.$refs.chapterTable.toggleRowSelection(node, selected);
                    if (node.children) {
                        traverse(node.children, selected);
                    }
                });
            };
            this.chapterData.forEach(row => {
                if (selection.includes(row)) {
                    if (row.children) {
                        traverse(row.children, true);
                    }
                } else {
                    if (row.children) {
                        traverse(row.children, false);
                    }
                }
            });
        },
        changeVideoNum(id){
            changeVideoNum({id:id}).then(res => {
                this.$message.success("修改成功");
                this.search();
            })
        }
    }
};
</script>

<style lang="scss" scoped>


</style>
